﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NewtonsoftUse.JsonUse;

namespace NewtonsoftUse
{

    public class LongDateTimeConvert : IsoDateTimeConverter
    {
        public LongDateTimeConvert() : base()
        {
            base.DateTimeFormat = "yyyy/MM/dd HH:mm";
        }
    }


    /// <summary>
    /// 用户
    /// <see cref=" MemberSerialization.OptOut" 默认值,类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore />
    /// <see cref=" MemberSerialization.OptIn" 默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化/>
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class User
    {
        [JsonProperty]
        public int Id { get; set; }

        [JsonProperty(PropertyName = "CName")]
        public string Name { get; set; }

        /// <summary>
        /// Json.Net提供了IsoDateTimeConverter日期转换这个类，
        /// 可以通过JsnConverter实现相应的日期转换
        /// </summary>
        [JsonProperty, JsonConverter(typeof(LongDateTimeConvert))]
        public DateTime DateTime { get; set; }

        /// <summary>
        /// <see cref="StringEnumConverter" 实现枚举转化/>
        /// </summary>
        [JsonProperty, JsonConverter(typeof(StringEnumConverter))]
        public UserType Type { get; set; }
        [JsonProperty]
        public List<NewUser> NewUsers { get; set; }


        public string IPhone { get; set; }
        /// <summary>
        /// 创建对象
        /// </summary>
        public static User CreateObj()
        {
            return new User
            {
                Id = 1,
                Name = "json使用",
                DateTime = DateTime.Now,
                Type = UserType.Zfb,
                IPhone = "123456",
                NewUsers = new List<NewUser>
                {
                    new NewUser { NewId  =1 },
                     new NewUser { NewId  =2 },
                      new NewUser { NewId  =3 },
                }
            };
        }
    }

    public class NewUser
    {
        public int NewId { get; set; }
    }

    public enum UserType
    {
        /// <summary>
        /// 微信用户
        /// </summary>
        Wx = 1,
        /// <summary>
        /// 支付宝用户
        /// </summary>
        Zfb
    }

    public class JsonToUser
    {

        public static void Use()
        {

            JsonSerializerSettings settings = new JsonSerializerSettings();
            {
                Console.WriteLine("处理json的基本特性");
                var json = JsonConvert.SerializeObject(User.CreateObj());
                Console.WriteLine("生成Json结果：" + json);
                var entity = JsonConvert.DeserializeObject<NewUser>(json);
                if (entity == null)
                {
                    //当无法序列化时，返回默认对象，并不是null
                    Console.WriteLine("获取不到对象值");
                }
                JObject obj = JObject.Parse(json);
                JToken name = obj["CName"];
                JToken phone = obj["IPhone"];
                JToken newUser = obj["NewUsers"];
                Console.WriteLine($"获取 CName:{obj["CName"]}");
                Console.WriteLine($"获取 IPhone:{obj["IPhone"]}");
                Console.WriteLine($"获取 newUser:{obj["newUser"]}");
            }
        }
    }
}
